# !/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import http.client
import json
import ssl
import urllib.parse
import urllib.request


def http_client():
    #忽略ssl证书
    ssl._create_default_https_context = ssl._create_unverified_context
    conn = http.client.HTTPSConnection("172.16.143.19:443")
    data = json.dumps({"method": "market_sendMsg", "userId": "e3b330b2-b782-48ae-97d4-3e92613797b8", "appTitle": "333",
                       "appContent": "333", "msgModel": "1"})
    print(data)
    headers = {"Content-Type": "application/json"}
    conn.request("POST", '/UnifyInterface/ser/outTransferInterface', data, headers)
    res = conn.getresponse()
    print(res.read().decode("utf-8"))


def http_client2():
    ssl._create_default_https_context = ssl._create_unverified_context
    conn = http.client.HTTPSConnection("172.16.143.19:443")
    data = json.dumps({"method": "market_queryMsg", "userId": "e3b330b2-b782-48ae-97d4-3e92613797b8", "msgId": 264992})
    print(data)
    headers = {"Content-Type": "application/json"}
    conn.request("POST", '/UnifyInterface/ser/outTransferInterface', data, headers)
    res = conn.getresponse()
    print(res.read().decode("utf-8"))


def http_urllib():
    ssl._create_default_https_context = ssl._create_unverified_context
    content = "嗨，好久不见您啦！若您遇到了操作问题或对产品有意见，欢迎登录APP“我的-设置-联系我们”反馈，飞贷欢迎您常回来看看！"
    code = str(base64.b64encode(content.encode("utf-8")), "utf-8")
    data = ('{"method": "mobile_sendMobile", "typeNo": "fdJZYX102", "mobile": "18771933975",'
            '"content": "%s"}' % code).encode('utf-8')
    print(str(data.decode('utf-8')))
    headers = {"Content-Type": "application/json"}
    req = urllib.request.Request("https://172.16.143.19/UnifyInterface/ser/outTransferInterface",
                                 data=data, headers=headers)
    res = urllib.request.urlopen(req)
    print(res.read().decode("utf-8"))


def http_urllib2():
    ssl._create_default_https_context = ssl._create_unverified_context
    data = '{"method": "mobile_sendCheckCode", "mobile": "18273197064"}'.encode('utf-8')
    print(str(data.decode('utf-8')))
    headers = {"Content-Type": "application/json"}
    req = urllib.request.Request("https://172.16.143.19/UnifyInterface/ser/outTransferInterface",
                                 data=data, headers=headers)
    res = urllib.request.urlopen(req)
    print(res.read().decode("utf-8"))


if __name__ == '__main__':
    http_client()
    # http_client2()
    # http_urllib()
    # http_urllib2()


